04. Demo: Matplotlib
PRDTM2-785 AI Trading C2 L4 Demo 1
Matplotlib Basics for Python Plots
Introduction to Matplotlib:
- A foundational plotting library in Python, inspired by MATLAB’s features.
- Offers easy-to-write Python code for customizable charts.
Installation and Importing:
- Install via
pip install matplotlib. - Import the pyplot submodule as
pltfor simplicity.
Setting Up Data:
- Uses data from APIs like Yahoo Finance, e.g., Apple stock data.
- Data organized in a DataFrame with dates as indices.
Creating Plots:
- Initiate a figure (canvas) using
plt.figure()with adjustable size. - Use
plt.plot()for line charts, specifying x (dates) and y (e.g., closing prices). - Customize colors and labels for clarity.
Enhancements:
- Add elements like title, axes labels, legends, and grid lines using
pltattributes (e.g.,plt.title()).
Multiple Data Sets on a Plot:
- Overlay different datasets by adding multiple
plt.plot()in one cell.
Advanced Plot Types:
- Create box plots with
plt.boxplot()and further customize appearance.
Customization Flexibility:
- Any plot attribute can be changed; refer to documentation for detailed customization options.